package eval

import (
	"showen/lex"
	"showen/obj"
	"showen/parse"
	"testing"
)

func TestCallWithCurrentContinuationBasic(t *testing.T) {
	env := obj.NewEnvironment()
	RegisterBuiltinProcedures(env)

	// Test basic usage: capturing and immediately calling the continuation
	input := `(call-with-current-continuation (lambda (k) (k 42)))`

	lexer := lex.NewLexer(input)
	tokens := lexer.ReadTokens()
	parser := parse.NewParser(tokens)
	expr, err := parser.ParseExpression()
	if err != nil {
		t.Fatalf("parser.ParseExpression() error = %v", err)
	}

	result, err := EvalExpression(expr, env)
	if err != nil {
		t.Fatalf("EvalExpression() error = %v", err)
	}

	expected := "42"
	if result.Inspect() != expected {
		t.Errorf("EvalExpression() = %v, want %v", result.Inspect(), expected)
	}
}

func TestCallWithCurrentContinuationEscape(t *testing.T) {
	env := obj.NewEnvironment()
	RegisterBuiltinProcedures(env)

	// Test using continuation as an escape mechanism
	input := `(call-with-current-continuation 
	            (lambda (escape) 
	              (+ 10 (escape 5))))`

	lexer := lex.NewLexer(input)
	tokens := lexer.ReadTokens()
	parser := parse.NewParser(tokens)
	expr, err := parser.ParseExpression()
	if err != nil {
		t.Fatalf("parser.ParseExpression() error = %v", err)
	}

	result, err := EvalExpression(expr, env)
	if err != nil {
		t.Fatalf("EvalExpression() error = %v", err)
	}

	expected := "5"
	if result.Inspect() != expected {
		t.Errorf("EvalExpression() = %v, want %v", result.Inspect(), expected)
	}
}
